﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Xml;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string cmd = TextBox1.Text;
        SqlExecutor myExec = new SqlExecutor();
        bool myResult = myExec.UpdateCmd(cmd);
        if (myResult) TextBox2.Text = "执行成功";
        else TextBox2.Text = "执行失败";
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string cmd = TextBox1.Text;
        SqlExecutor myExec = new SqlExecutor();
        DataTable myResult = myExec.SelectCmd(cmd);
        GridView1.DataSource = myResult.DefaultView;
        GridView1.DataBind();
    }

    //添加用户
    protected void Button3_Click(object sender, EventArgs e)
    {
        UserInfo newUser = new UserInfo();
        if (newUser.Add(UserIDTB.Text, NicknameTB.Text, PasswordTB.Text, int.Parse(PopedomTB.Text)))
        { Response.Write("添加成功"); }
        else
        { Response.Write("添加失败"); }
    }

    //修改用户信息
    protected void Button4_Click(object sender, EventArgs e)
    {
        UserInfo newUser = new UserInfo();
        string nickname;
        string password;
        int popedom;
        if (NicknameTB.Text == "") { nickname = null; }
        else { nickname = NicknameTB.Text; }
        if (PasswordTB.Text == "") { password = null; }
        else { password = PasswordTB.Text; }
        if (PopedomTB.Text == "") { popedom = -1; }
        else { popedom = int.Parse(PopedomTB.Text); }
        if (newUser.Update(UserIDTB.Text, nickname, password, popedom))
        { Response.Write("修改成功"); }
        else
        { Response.Write("修改失败"); }
    }

    //读取用户信息
    protected void Button5_Click(object sender, EventArgs e)
    {
        UserInfo user = new UserInfo();
        user.UserID = UserIDTB.Text;
        if (user.Load())
        {
            NicknameTB.Text = user.Nickname;
            PopedomTB.Text = user.Popedom.ToString();
            Response.Write("读取成功");
        }
        else { Response.Write("读取失败！"); }
    }

    //添加影片
    protected void AddMovieBtn_Click(object sender, EventArgs e)
    {
        Movie newMovie = new Movie();
        if (newMovie.Add(nameTB.Text,
                                ownerTB.Text,
                                introductionTB.Text,
                                urlTB.Text,
                                catalogTB.Text.Split(','),
                                tagTB.Text.Split(','))
            ) { Response.Write("添加成功"); }
        else { Response.Write("添加失败"); }                        
    }

    //修改影片
    protected void UpdateMovieBtn_Click(object sender, EventArgs e)
    {
        Movie movie = new Movie();
        if (movie.Update(int.Parse(idTB.Text),
                                nameTB.Text,
                                introductionTB.Text,
                                catalogTB.Text.Split(','),
                                tagTB.Text.Split(','))
            ) { Response.Write("修改成功"); }
        else { Response.Write("修改失败"); }
    }

    //读取影片信息
    protected void LoadMovieBtn_Click(object sender, EventArgs e)
    {
        Movie movie = new Movie();
        movie.MovieID = int.Parse(idTB.Text);
        if (movie.Load()) { Response.Write("读取成功"); }
        else { Response.Write("读取失败"); return; }

        nameTB.Text = movie.MovieName;
        ownerTB.Text = movie.OwnerID;
        urlTB.Text = movie.URL;
        catalogTB.Text=string.Join(",",movie.Catalogs);
        tagTB.Text = string.Join(",", movie.Tag);
        datetimeTB.Text = movie.UploadDatetime.ToString();
        introductionTB.Text = movie.Introduction;
        focusTB.Text = movie.Focus.ToString();
    }

    //删除电影
    protected void DelMovieBtn_Click(object sender, EventArgs e)
    {
        Movie movie = new Movie();
        if (movie.Delete(int.Parse(idTB.Text))) { Response.Write("删除成功"); }
        else { Response.Write("删除失败"); }
    }

    //点击影片
    protected void upBtn_Click(object sender, EventArgs e)
    {
        Movie movie = new Movie();
        if (movie.UpMovie(int.Parse(idTB.Text))) { Response.Write("点击成功"); }
        else { Response.Write("点击失败"); }
    }

    //添加节点
    protected void Button6_Click(object sender, EventArgs e)
    {
        if (TreeViewTest.SelectedNode != null )
        {
            XmlDocument myXml = new XmlDocument();//使用dom读取xml信息            
            string path = Server.MapPath("~/Leadin.xml");//把服务器虚拟路径转为物理路径        
            myXml.Load(path);//读取XML文件    
            ///Response.Write(TreeViewTest.SelectedNode.ValuePath);

            string xpath = XpathTransfer(TreeViewTest.SelectedNode.ValuePath);//将树的路径转为XPATH
            XmlNode currentNode = myXml.SelectSingleNode(xpath);//通过XPATH找到要操作的
            XmlElement newNode = myXml.CreateElement("node");//创建新节点
            newNode.SetAttribute("name", TextBox3.Text);//为节点添加属性
            currentNode.AppendChild(newNode);//将新节点添加到当前节点
            myXml.Save(path);
            //XmlDataSourceTest.DataFile = "~/Leadin.xml";
            //TreeViewTest.DataSourceID = "XmlDataSourceTest";
            Response.Redirect(Request.Url.ToString()); //刷新页面
        }
    }

    //将树路径转化为XPATH
    protected string XpathTransfer(string path)
    {
        string xpath = "";
        string[] names = path.Split('/');
        xpath += "/leadin";
        for (int i = 1; i < names.Length; i++)
        {
            xpath += "/node[@name='" + names[i] + "']";
        }
        return xpath;
    }

    //修改节点
    protected void Button7_Click(object sender, EventArgs e)
    {
        XmlDocument myXml = new XmlDocument();//使用dom读取xml信息            
        string path = Server.MapPath("~/Leadin.xml");//把服务器虚拟路径转为物理路径        
        myXml.Load(path);//读取XML文件    
        //Response.Write(TreeViewTest.SelectedNode.ValuePath);

        string xpath = XpathTransfer(TreeViewTest.SelectedNode.ValuePath);//将树的路径转为XPATH
        XmlNode currentNode = myXml.SelectSingleNode(xpath);//通过XPATH找到要操作的
        currentNode.Attributes["name"].Value = TextBox3.Text;
        myXml.Save(path);//保存修改
        Response.Redirect(Request.Url.ToString()); //刷新页面
    }

    //删除节点
    protected void Button8_Click(object sender, EventArgs e)
    {
        XmlDocument myXml = new XmlDocument();//使用dom读取xml信息            
        string path = Server.MapPath("~/Leadin.xml");//把服务器虚拟路径转为物理路径        
        myXml.Load(path);//读取XML文件 
        string xpath = XpathTransfer(TreeViewTest.SelectedNode.ValuePath);//将树的路径转为XPATH
        XmlNode currentNode = myXml.SelectSingleNode(xpath);//通过XPATH找到要操作的
        currentNode.ParentNode.RemoveChild(currentNode);//通过父节点移除自己
        myXml.Save(path);//保存修改
        Response.Redirect(Request.Url.ToString()); //刷新页面
    }

    //节点上移
    protected void Button9_Click(object sender, EventArgs e)
    {
        XmlDocument myXml = new XmlDocument();//使用dom读取xml信息            
        string path = Server.MapPath("~/Leadin.xml");//把服务器虚拟路径转为物理路径        
        myXml.Load(path);//读取XML文件 
        string xpath = XpathTransfer(TreeViewTest.SelectedNode.ValuePath);//将树的路径转为XPATH
        XmlNode currentNode = myXml.SelectSingleNode(xpath);//通过XPATH找到要操作的
        if (currentNode.ParentNode.FirstChild.Equals(currentNode)) return;//如果节点已经是第一子节点则返回
        XmlNode tempNode = currentNode.PreviousSibling.Clone();//创建上一节点的副本
        currentNode.ParentNode.ReplaceChild(currentNode, currentNode.PreviousSibling);//先把上一节点替换为自己
        currentNode.ParentNode.InsertAfter(tempNode, currentNode);//把自己后面插入上一节点的副本
        myXml.Save(path);//保存修改
        Response.Redirect(Request.Url.ToString()); //刷新页面
    }

    //节点下移
    protected void Button10_Click(object sender, EventArgs e)
    {
        XmlDocument myXml = new XmlDocument();//使用dom读取xml信息            
        string path = Server.MapPath("~/Leadin.xml");//把服务器虚拟路径转为物理路径        
        myXml.Load(path);//读取XML文件 
        string xpath = XpathTransfer(TreeViewTest.SelectedNode.ValuePath);//将树的路径转为XPATH
        XmlNode currentNode = myXml.SelectSingleNode(xpath);//通过XPATH找到要操作的
        if (currentNode.ParentNode.LastChild.Equals(currentNode)) return;//如果节点已经是最后子节点则返回
        XmlNode tempNode = currentNode.NextSibling.Clone();//创建下一节点的副本
        currentNode.ParentNode.ReplaceChild(currentNode, currentNode.NextSibling);//先把上一节点替换为自己
        currentNode.ParentNode.InsertBefore(tempNode, currentNode);//把自己后面插入上一节点的副本
        myXml.Save(path);//保存修改
        Response.Redirect(Request.Url.ToString()); //刷新页面
    }
}
